package interview;

/**
 * Created by QingYun on 2017/10/18.
 */
public class FourThreadTest {

    private Object lock = new Object();

    public static  int count;



    public static void main(String[] args) throws InterruptedException {

        FourThreadTest abc = new FourThreadTest();


        for (int i=0;i<15;i++){

            new Thread(abc.new Run("C", 3)).start();

            new Thread(abc.new Run("D", 4)).start();

            new Thread(abc.new Run("A", 1)).start();

            new Thread(abc.new Run("B", 2)).start();

            count=0;
            Thread.sleep(3000);

        }



    }

    class Run implements Runnable {

        private String _name = "";

        private int _threadNum;



        public Run(String name, int threadNum) {

            _name = name;

            _threadNum = threadNum;

        }



        @Override

        public void run() {

            synchronized (lock) {

                while (true) {

                    if (count % 4 == _threadNum - 1) {

                        System.out.print(this._name);

                        count++;

                        lock.notifyAll();

                        break;

                    } else {

                        try {

                            lock.wait();

                        } catch (InterruptedException e) {

                            e.printStackTrace();

                        }

                    }

                }



            }

        }

    }




}
